package com.fusion.lottery.mapper.auth;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.fusion.lottery.entity.auth.Authorization;
import com.fusion.lottery.entity.auth.Role;
import com.fusion.lottery.entity.auth.Source;
import com.fusion.lottery.entity.auth.User;

public interface RoleMapper {
	
	/**
	 * 
	 * @param user
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@Select({
		"<script>",
		"SELECT * FROM t_auth_role ",
		" <when test='pageSize > 0'> limit #{start},#{pageSize}</when> ",
		"</script>"
		})
	List<Role> list(Role role,@Param("start")long start,@Param("pageSize")long pageSize);
	
	/**
	 * 
	 * @param role
	 * @return
	 */
	@Select("SELECT count(1) FROM t_auth_role")
	long count(Role role);
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@Select("SELECT * FROM t_auth_role WHERE id = #{id}")
	Role get(long id);
	
	/**
	 * 添加角色
	 * @param role
	 */
	@Insert("INSERT INTO t_auth_role(name,code,status) "
			+ "VALUES(#{name}, #{code}, #{status})")
	@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
	void insert(Role role);
	
	/**
	 * 删除角色
	 * @param userId
	 */
	@Delete("DELETE FROM t_auth_role WHERE id =#{id}")
	void delete(long id);
	
	/**
	 * 修改角色信息
	 * @param role
	 */
	@Update({"<script>",
		"update t_auth_role set id=id ",
		" <when test='name != null '> ,name=#{name}</when> ",
		" <when test='code != null '> ,code=#{code}</when> ",
		" <when test='status != null '> ,status=#{status}</when> ",
		" where id= #{id}",
		"</script>"})
	void update(Role role);
	
	/**
	 * 获取角色的权限
	 * @param userId
	 * @return
	 */
	@Select("SELECT t.id,t.`name` FROM t_auth_resource_assignments s,t_auth_security_resources t "
			+ "WHERE s.sourceId = t.id AND s.roleId = #{roleId}")
	List<Source> getPermission(long roleId);
	
	/**
	 * 获取角色下的用户
	 * @param roleId
	 * @return
	 */
	@Select("SELECT u.* FROM t_auth_user u,t_auth_authorization a WHERE a.userId = u.id AND a.roleId = #{roleId}")
	List<User> getUser(long roleId);
	
	/**
	 * 给用户分配角色
	 * @param authorization
	 */
	@Insert("INSERT INTO t_auth_authorization(userId,roleId) values(#{userId}, #{roleId})")
	void grantRole(Authorization authorization);
	
	/**
	 * 回收角色
	 * @param authorization
	 */
	@Delete("DELETE FROM t_auth_authorization where userId = #{userId} and roleId = #{roleId}")
	void recoverRole(Authorization authorization);
	
	/**
	 * 回收权限
	 * @param roleId
	 */
	@Delete("DELETE FROM t_auth_resource_assignments where roleId = #{roleId}")
	void recoverPermission(long roleId);
	
	/**
	 * 分配权限
	 * @param roleId
	 * @param sourceIds
	 */
	@Insert({"<script>",
			"INSERT INTO t_auth_resource_assignments(sourceId,roleId) ",
			"select id,#{roleId} from t_auth_security_resources where id in (${sourceIds})",
			"</script>"
			})
//	void grantPermission(@Param("roleId")long roleId,@Param("sourceIds") List<String> sourceIds);
	void grantPermission(@Param("roleId")long roleId,@Param("sourceIds") String sourceIds);
}
